#!/bin/bash

# Make sure we are root, if we cant stop the services, the results are a bit hit and miss.
if [ "$(id -u)" != "0" ]; then
  echo -e "You need to be root to run this command...\n"
  exit 1
fi

# Make the disk RW
if [ -d /boot/firmware ]; then
  (sudo mount -o remount,rw / 2>/dev/null ; sudo mount -o remount,rw /boot/firmware 2>/dev/null)
else
  mount -o remount,rw /boot
  mount -o remount,rw /
fi

# Host Files Update
/usr/local/sbin/HostFilesUpdate.sh

# Update the Dashboard from GIT
gitBranchDash=$(git --work-tree=/var/www/dashboard --git-dir=/var/www/dashboard/.git rev-parse --abbrev-ref HEAD)
git --work-tree=/var/www/dashboard --git-dir=/var/www/dashboard/.git pull origin ${gitBranchDash}

# Stop all the services...
systemctl stop pistar-watchdog.service > /dev/null 2>&1
systemctl stop mmdvmhost.service > /dev/null 2>&1
systemctl stop dstarrepeater.service > /dev/null 2>&1
sleep 2
systemctl stop ircddbgateway.service > /dev/null 2>&1
systemctl stop dapnetgateway.service > /dev/null 2>&1
if [ -f /lib/systemd/system/m17gateway.service ]; then
  systemctl stop m17gateway.service > /dev/null 2>&1
fi
systemctl stop p25gateway.service > /dev/null 2>&1
systemctl stop p25parrot.service > /dev/null 2>&1
systemctl stop nxdngateway.service > /dev/null 2>&1
systemctl stop nxdnparrot.service > /dev/null 2>&1
systemctl stop ysfgateway.service > /dev/null 2>&1
systemctl stop ysfparrot.service > /dev/null 2>&1
systemctl stop ysf2dmr.service > /dev/null 2>&1
systemctl stop ysf2p25.service > /dev/null 2>&1
systemctl stop ysf2nxdn.service > /dev/null 2>&1
systemctl stop dmr2ysf.service > /dev/null 2>&1
systemctl stop dmr2nxdn.service > /dev/null 2>&1
systemctl stop dmrgateway.service > /dev/null 2>&1
systemctl stop timeserver.service > /dev/null 2>&1
if [ -f /lib/systemd/system/aprsgateway.service ]; then
  systemctl stop aprsgateway.service > /dev/null 2>&1
fi

# Update the Binaries
gitBranchBin=$(git --work-tree=/usr/local/bin --git-dir=/usr/local/bin/.git rev-parse --abbrev-ref HEAD)
git --work-tree=/usr/local/bin --git-dir=/usr/local/bin/.git pull origin ${gitBranchBin}

# Update the Binaries (sbin)
gitBranchSbin=$(git --work-tree=/usr/local/sbin --git-dir=/usr/local/sbin/.git rev-parse --abbrev-ref HEAD)
git --work-tree=/usr/local/sbin --git-dir=/usr/local/sbin/.git pull origin ${gitBranchSbin}

# Purge the logs older than 2 days
rm -f $(find /var/log/pi-star/*.log -type f -mtime +0 -print)
rm -f $(find /var/log/ -type f -mtime +0 -print | grep .gz)

# Shrink NxinX error log to stop it getting out of hand
echo "$(tail -500 /var/log/nginx/error.log)" > /var/log/nginx/error.log

# Pre-Fix some config in MMDVMHost for update purposes.
mmdvmHostVer=`MMDVMHost -v | awk '{print $3}' | cut -c 1-8`
needsUpdate=`grep -c Gwy /etc/mmdvmhost`
if [ ${mmdvmHostVer} \> 20171031 ] && [ ${needsUpdate} \> 0 ]; then
        # Config needs to be updated, add in the changes here
        sed -i "/GwyAddress=/c\\GatewayAddress=127.0.0.1" /etc/mmdvmhost
        sed -i "/GwyPort=/c\\GatewayPort=4200" /etc/mmdvmhost
fi

# Fix up new P25Gateway Config Hostfile setup
if [[ $(/usr/local/bin/P25Gateway --version | awk '{print $3}' | cut -c -8) -gt "20180108" ]]; then
	sed -i 's/Hosts=\/usr\/local\/etc\/P25Hosts.txt/HostsFile1=\/usr\/local\/etc\/P25Hosts.txt\nHostsFile2=\/usr\/local\/etc\/P25HostsLocal.txt/g' /etc/p25gateway
fi
if [ ! -f /root/P25Hosts.txt ]; then
	touch /root/P25Hosts.txt
fi

# If we are ready to use the new DMRGateway
if [[ $(/usr/local/bin/DMRGateway --version | awk '{print $3}' | cut -c -8) -gt "20170924" ]] && [[ $(grep -c "\[DMR Network 3\]" /etc/dmrgateway) -eq "1" ]] && [[ ! -f /usr/local/etc/DMR_Audio/no_NO.indx ]]; then
	curl --fail -o /usr/local/etc/DMR_Audio/de_DE.ambe -s https://raw.githubusercontent.com/g4klx/DMRGateway/master/Audio/de_DE.ambe
	curl --fail -o /usr/local/etc/DMR_Audio/de_DE.indx -s https://raw.githubusercontent.com/g4klx/DMRGateway/master/Audio/de_DE.indx
	curl --fail -o /usr/local/etc/DMR_Audio/dk_DK.ambe -s https://raw.githubusercontent.com/g4klx/DMRGateway/master/Audio/dk_DK.ambe
	curl --fail -o /usr/local/etc/DMR_Audio/dk_DK.indx -s https://raw.githubusercontent.com/g4klx/DMRGateway/master/Audio/dk_DK.indx
	curl --fail -o /usr/local/etc/DMR_Audio/en_GB.ambe -s https://raw.githubusercontent.com/g4klx/DMRGateway/master/Audio/en_GB.ambe
	curl --fail -o /usr/local/etc/DMR_Audio/en_GB.indx -s https://raw.githubusercontent.com/g4klx/DMRGateway/master/Audio/en_GB.indx
	curl --fail -o /usr/local/etc/DMR_Audio/en_US.ambe -s https://raw.githubusercontent.com/g4klx/DMRGateway/master/Audio/en_US.ambe
	curl --fail -o /usr/local/etc/DMR_Audio/en_US.indx -s https://raw.githubusercontent.com/g4klx/DMRGateway/master/Audio/en_US.indx
	curl --fail -o /usr/local/etc/DMR_Audio/es_ES.ambe -s https://raw.githubusercontent.com/g4klx/DMRGateway/master/Audio/es_ES.ambe
	curl --fail -o /usr/local/etc/DMR_Audio/es_ES.indx -s https://raw.githubusercontent.com/g4klx/DMRGateway/master/Audio/es_ES.indx
	curl --fail -o /usr/local/etc/DMR_Audio/fr_FR.ambe -s https://raw.githubusercontent.com/g4klx/DMRGateway/master/Audio/fr_FR.ambe
	curl --fail -o /usr/local/etc/DMR_Audio/fr_FR.indx -s https://raw.githubusercontent.com/g4klx/DMRGateway/master/Audio/fr_FR.indx
	curl --fail -o /usr/local/etc/DMR_Audio/it_IT.ambe -s https://raw.githubusercontent.com/g4klx/DMRGateway/master/Audio/it_IT.ambe
	curl --fail -o /usr/local/etc/DMR_Audio/it_IT.indx -s https://raw.githubusercontent.com/g4klx/DMRGateway/master/Audio/it_IT.indx
	curl --fail -o /usr/local/etc/DMR_Audio/no_NO.ambe -s https://raw.githubusercontent.com/g4klx/DMRGateway/master/Audio/no_NO.ambe
	curl --fail -o /usr/local/etc/DMR_Audio/no_NO.indx -s https://raw.githubusercontent.com/g4klx/DMRGateway/master/Audio/no_NO.indx
	curl --fail -o /usr/local/etc/DMR_Audio/pl_PL.ambe -s https://raw.githubusercontent.com/g4klx/DMRGateway/master/Audio/pl_PL.ambe
	curl --fail -o /usr/local/etc/DMR_Audio/pl_PL.indx -s https://raw.githubusercontent.com/g4klx/DMRGateway/master/Audio/pl_PL.indx
	curl --fail -o /usr/local/etc/DMR_Audio/se_SE.ambe -s https://raw.githubusercontent.com/g4klx/DMRGateway/master/Audio/se_SE.ambe
	curl --fail -o /usr/local/etc/DMR_Audio/se_SE.indx -s https://raw.githubusercontent.com/g4klx/DMRGateway/master/Audio/se_SE.indx	
fi

if [[ $(grep Version /etc/pistar-release | awk '{print $3}' | cut -c 1) -eq 4 ]]; then
        if [[ $(cat /etc/ircddbgateway | grep -o "xlx" | wc -l) -eq 0 ]]; then
                echo "xlxEnabled=0" >> /etc/ircddbgateway
                echo "xlxOverrideLocal=0" >> /etc/ircddbgateway
                echo "xlxHostsFileUrl=" >> /etc/ircddbgateway
        fi

        if [[ $(cat /etc/ircddbgateway | grep -o "aprsPassword" | wc -l) -eq 0 ]]; then
                echo "aprsPassword=00000" >> /etc/ircddbgateway
        fi

        if [[ $(cat /etc/ircddbgateway | grep -o "mobileGPS" | wc -l) -eq 0 ]]; then
                echo "mobileGPSEnabled=0" >> /etc/ircddbgateway
                echo "mobileGPSAddress=127.0.0.1" >> /etc/ircddbgateway
                echo "mobileGPSPort=7834" >> /etc/ircddbgateway
        fi

        if [[ $(cat /etc/dstarrepeater | grep -o "mmdvmRXInvert" | wc -l) -eq 0 ]]; then
                echo "mmdvmRXInvert=0" >> /etc/dstarrepeater
                echo "mmdvmTXInvert=0" >> /etc/dstarrepeater
                echo "mmdvmPTTInvert=0" >> /etc/dstarrepeater
                echo "mmdvmTXDelay=50" >> /etc/dstarrepeater
                echo "mmdvmRXLevel=100" >> /etc/dstarrepeater
                echo "mmdvmTXLevel=100" >> /etc/dstarrepeater
        fi
fi

# Start all the services...
systemctl start ircddbgateway.service > /dev/null 2>&1
systemctl start dmrgateway.service > /dev/null 2>&1
systemctl start dapnetgateway.service > /dev/null 2>&1
if [ -f /lib/systemd/system/m17gateway.service ]; then
  systemctl start m17gateway.service > /dev/null 2>&1
fi
systemctl start p25gateway.service > /dev/null 2>&1
systemctl start p25parrot.service > /dev/null 2>&1
systemctl start nxdngateway.service > /dev/null 2>&1
systemctl start nxdnparrot.service > /dev/null 2>&1
systemctl start ysfgateway.service > /dev/null 2>&1
systemctl start ysfparrot.service > /dev/null 2>&1
systemctl start ysf2dmr.service > /dev/null 2>&1
systemctl start ysf2p25.service > /dev/null 2>&1
systemctl start ysf2nxdn.service > /dev/null 2>&1
systemctl start dmr2ysf.service > /dev/null 2>&1
systemctl start dmr2nxdn.service > /dev/null 2>&1
systemctl start timeserver.service > /dev/null 2>&1
if [ -f /lib/systemd/system/aprsgateway.service ]; then
  systemctl start aprsgateway.service > /dev/null 2>&1
fi
systemctl start mmdvmhost.service > /dev/null 2>&1
systemctl start dstarrepeater.service > /dev/null 2>&1
systemctl start pistar-watchdog.service > /dev/null 2>&1

# Fix nginx unit file
if ! [ $(cat /lib/systemd/system/nginx.service | grep -o "mkdir") ]; then
  sed -i '\/PIDFile=\/run\/nginx.pid/a ExecStartPre=\/bin\/mkdir -p \/var\/log\/nginx' /lib/systemd/system/nginx.service
	systemctl daemon-reload
	systemctl restart nginx.service
fi

exit 0
